package cn.com.fssg.platform.base.handler.impl.cl.kaoqin;

import cn.com.fssg.platform.base.handler.InterfaceHandler;
import cn.com.fssg.platform.biz.BizUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

@Component
public class ClKaoQinSignInInterfaceHandler implements InterfaceHandler {
//    private NamedParameterJdbcTemplate jdbcTemplate = BizUtil.cur().tenantJdbcTemplate("CJRLHHConnString");
    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;
    private SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    private final int SIGN_IN = 1;
    private final int SIGN_OUT = 2;

    @Override
    public Object run(HttpServletRequest request, JSONObject jsonObject) throws Exception {
        String dyqId = jsonObject.getString("dyqId");
        // 1.签到  2.签退
        Integer status = jsonObject.getInteger("status");
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("dyqId",dyqId);
        try {
            String queryDyq = "select * from cl_dyq where id = :dyqId";
            Map<String, Object> dyqMap = jdbcTemplate.queryForMap(queryDyq, paramMap);
            if (ObjectUtils.isEmpty(dyqMap)) {
                return "未找到对应的代用券记录";
            }
            String queryCheckIn = "select * from cl_sign_record where dyq_id = :dyqId and to_char(date,'yyyy-MM-dd') = to_char(CURRENT_DATE,'yyyy-MM-dd')";
            List<Map<String, Object>> queryResult = jdbcTemplate.queryForList(queryCheckIn, paramMap);
            String time = timeFormat.format(new Date());
            if (CollectionUtils.isEmpty(queryResult) && status == 1) {
                String insert = "insert into cl_sign_record (id, org_id, cjr_name, cjr_idcard, date," +
                        " status, org_name, sign_in_time, create_by, create_time," +
                        " update_by, update_time, bud_id, dyq_id) values" +
                        " (:id, :org_id, :cjr_name, :cjr_idcard, current_date," +
                        " :status, :org_name, :sign_in_time, :create_by, :create_time," +
                        " :update_by, :update_time, :bud_id, :dyq_id)";
                Map<String, Object> insertMap = new HashMap<>();
                insertMap.put("id", BizUtil.cur().getSnowflakeIdStr());
//                insertMap.put("id", UUID.randomUUID());
                insertMap.put("org_id", dyqMap.get("org_id"));
                insertMap.put("cjr_name", dyqMap.get("cjr_name"));
                insertMap.put("cjr_idcard", dyqMap.get("cjr_idcard"));
                insertMap.put("status", SIGN_IN);
                insertMap.put("org_name", dyqMap.get("org_name"));
                insertMap.put("sign_in_time", time);
                insertMap.put("create_by", BizUtil.cur().curUserId());
//                insertMap.put("create_by", "111111");
                insertMap.put("create_time", time);
                insertMap.put("update_by", BizUtil.cur().curUserId());
//                insertMap.put("update_by", "111111");
                insertMap.put("update_time", time);
                insertMap.put("bud_id", dyqMap.get("bus_id"));
                insertMap.put("dyq_id", dyqMap.get("id"));
                jdbcTemplate.update(insert, insertMap);
                return "签到成功";
            } else if (!CollectionUtils.isEmpty(queryResult)){
                if (status == 1) {
                    return "请勿重复签到";
                } else if (status == 2) {
                    String update = "update cl_sign_record set status = :status, sign_out_time = :sign_out_time," +
                            " update_by = :update_by, update_time = :update_time " +
                            " where id = :dyqId and to_char(date,'yyyy-MM-dd') = to_char(CURRENT_DATE,'yyyy-MM-dd')";
                    Map<String,Object> updateMap = new HashMap<>();
                    updateMap.put("status", SIGN_OUT);
                    updateMap.put("sign_out_time", time);
                    updateMap.put("update_by", BizUtil.cur().curUserId());
//                    updateMap.put("update_by", "22222");
                    updateMap.put("update_time", time);
                    updateMap.put("dyqId", dyqId);
                    jdbcTemplate.update(update, updateMap);
                    return "签退成功";
                }

            }
        } catch (Exception e) {
            return "jdbc:" + jdbcTemplate + "; message:" + e.getMessage();
        }
        return null;
    }
}
